package com.zhht.checkcop.demo;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.mysql.jdbc.StringUtils;
import com.zhht.checkcop.db.MysqlDb;
import com.zhht.checkcop.db.StaticDBMap;
import com.zhht.checkcop.parser.ParserMovieInfoByUpdateTime;

import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.processor.PageProcessor;

public class MovieUpdateTime implements PageProcessor{
	private final static Logger log= Logger.getLogger(MovieUpdateTime.class);
	
	private Site site = Site.me().setCycleRetryTimes(3).setSleepTime(1500).setTimeOut(5000);
	private List<String> novelListUrl;
	public MovieUpdateTime(){}
	public MovieUpdateTime(List<String> novelListUrl){
		this.novelListUrl=novelListUrl;
	}
	
	private Boolean bool=true;
	
	public void process(Page page) {
    	try{
    		if(bool){
    			bool=false;
    			page.addTargetRequests(novelListUrl);
    		}else{
    			
		    	page.putField("infoUrl", page.getRequest().getUrl());
		    	String updateTime = page.getHtml().xpath("//div[@class='info']/span[2]/text()").get();
		    	
		    	if(!StringUtils.isNullOrEmpty(updateTime)){
		    		updateTime=updateTime.replace("发布时间：", "");
		    	}
		    	page.putField("updateTime", updateTime);
    		}
		} catch (Exception e) {
			e.printStackTrace();
		}
    }

    public Site getSite() {
        return site;
    }

    public static void main(String[] args) {
//    	SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss");
		SynchronizeNewNovel(args);
//		Mail.send("时间："+sdf.format(new Date())+"  验证 sql:select count(1) from novel_info where source = 1 and thisSyn = 0 and d = 0  失败同步书籍数量:");
    }
	private static void SynchronizeNewNovel(String[] args) {
		try{
        	MysqlDb mysqldb = new MysqlDb();
        	mysqldb.connect();
        	
        	StaticDBMap.existNovelNameMap = mysqldb.query("select id,infoUrl from movie_info where d = 0 ",new ArrayList(),"infoUrl");
        	
        	int thread = args!=null&&args.length>0?Integer.parseInt(args[0]):20;
        	List<String> strList1 = mysqldb.queryListStr("select infoUrl from movie_info where classifyId = 1 and d = 0 ");
        	List<String> strList2 = mysqldb.queryListStr("select infoUrl from movie_info where classifyId = 2 and d = 0 ");
        	List<String> strList3 = mysqldb.queryListStr("select infoUrl from movie_info where classifyId = 3 and d = 0 ");
        	List<String> strList4 = mysqldb.queryListStr("select infoUrl from movie_info where classifyId = 4 and d = 0 ");
        	List<String> strList5 = mysqldb.queryListStr("select infoUrl from movie_info where classifyId = 5 and d = 0 ");
        	List<String> strList6 = mysqldb.queryListStr("select infoUrl from movie_info where classifyId = 6 and d = 0 ");
        	List<String> strList7 = mysqldb.queryListStr("select infoUrl from movie_info where classifyId = 7 and d = 0 ");
        	List<String> strList8 = mysqldb.queryListStr("select infoUrl from movie_info where classifyId = 8 and d = 0 ");
        	List<String> strList9 = mysqldb.queryListStr("select infoUrl from movie_info where classifyId = 9 and d = 0 ");
        	List<String> strList10 = mysqldb.queryListStr("select infoUrl from movie_info where classifyId = 10 and d = 0 ");
        	List<String> strList11 = mysqldb.queryListStr("select infoUrl from movie_info where classifyId = 11 and d = 0 ");
        	List<String> strList12 = mysqldb.queryListStr("select infoUrl from movie_info where classifyId = 12 and d = 0 ");
        	mysqldb.close();
        	
        	Spider.create(new MovieUpdateTime(strList1)).addUrl("https://www.piaohua.com/").addPipeline(new ParserMovieInfoByUpdateTime("1")).thread(thread).run();
        	log.info("动作片已完成");
        	Spider.create(new MovieUpdateTime(strList2)).addUrl("https://www.piaohua.com/").addPipeline(new ParserMovieInfoByUpdateTime("2")).thread(thread).run();
        	log.info("喜剧片已完成");
        	Spider.create(new MovieUpdateTime(strList3)).addUrl("https://www.piaohua.com/").addPipeline(new ParserMovieInfoByUpdateTime("3")).thread(thread).run();
        	log.info("爱情片已完成");
        	Spider.create(new MovieUpdateTime(strList4)).addUrl("https://www.piaohua.com/").addPipeline(new ParserMovieInfoByUpdateTime("4")).thread(thread).run();
        	log.info("科幻片已完成");
        	Spider.create(new MovieUpdateTime(strList5)).addUrl("https://www.piaohua.com/").addPipeline(new ParserMovieInfoByUpdateTime("5")).thread(thread).run();
        	log.info("剧情片已完成");
        	Spider.create(new MovieUpdateTime(strList6)).addUrl("https://www.piaohua.com/").addPipeline(new ParserMovieInfoByUpdateTime("6")).thread(thread).run();
        	log.info("悬疑片已完成");
        	Spider.create(new MovieUpdateTime(strList7)).addUrl("https://www.piaohua.com/").addPipeline(new ParserMovieInfoByUpdateTime("7")).thread(thread).run();
        	log.info("战争片已完成");
        	Spider.create(new MovieUpdateTime(strList8)).addUrl("https://www.piaohua.com/").addPipeline(new ParserMovieInfoByUpdateTime("8")).thread(thread).run();
        	log.info("恐怖片已完成");
        	Spider.create(new MovieUpdateTime(strList9)).addUrl("https://www.piaohua.com/").addPipeline(new ParserMovieInfoByUpdateTime("9")).thread(thread).run();
        	log.info("灾难片已完成");
        	Spider.create(new MovieUpdateTime(strList10)).addUrl("https://www.piaohua.com/").addPipeline(new ParserMovieInfoByUpdateTime("10")).thread(thread).run();
        	log.info("连续剧已完成");
        	Spider.create(new MovieUpdateTime(strList11)).addUrl("https://www.piaohua.com/").addPipeline(new ParserMovieInfoByUpdateTime("11")).thread(thread).run();
        	log.info("动漫已完成");
        	Spider.create(new MovieUpdateTime(strList12)).addUrl("https://www.piaohua.com/").addPipeline(new ParserMovieInfoByUpdateTime("12")).thread(thread).run();
        	log.info("综艺片已完成");
    	}catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	//链接url下载图片
    private boolean downloadPicture(String urlList) {
        URL url = null;
        try {
            url = new URL(urlList);
            URLConnection conn = url.openConnection();
            conn.setReadTimeout(1500);
            conn.setConnectTimeout(1500);
            conn.connect();
            return true;
        } catch (MalformedURLException e) {
        } catch (IOException e) {
        } catch (Exception e) {
        }
        return false;
    }
}
